package com.itheima.service.impl;

import com.itheima.dao.store.CompanyMapper;
import com.itheima.dao.store.CompanyMapper;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {
    SqlSession sqlSession =null;
    Integer rst;
    @Override
    public List<Company> findAll() {
         sqlSession = MapperFactory.getSqlSession();
        CompanyMapper mapperList = sqlSession.getMapper(CompanyMapper.class);
        mapperList.findAll();

        TransactionUtil.close(sqlSession);
        return (List<Company>) mapperList;
    }

    @Override
    public Company findById(String id) {
        sqlSession = MapperFactory.getSqlSession();
        CompanyMapper mapper= sqlSession.getMapper(CompanyMapper.class);

        Company company = mapper.findById(id);

        TransactionUtil.close(sqlSession);
        return company;
    }

    @Override
    public int add(Company company) {
        sqlSession = MapperFactory.getSqlSession();
        CompanyMapper mapper= sqlSession.getMapper(CompanyMapper.class);
        rst = mapper.add(company);
        TransactionUtil.close(sqlSession);
        return rst;
    }

    @Override
    public int update(Company company) {
        sqlSession = MapperFactory.getSqlSession();
        CompanyMapper mapper= sqlSession.getMapper(CompanyMapper.class);
        rst = mapper.update(company);
        TransactionUtil.close(sqlSession);
        return rst;
    }

    @Override
    public int delete(Company company) {
        sqlSession = MapperFactory.getSqlSession();
        CompanyMapper mapper= sqlSession.getMapper(CompanyMapper.class);
        rst = mapper.delete(company);
        TransactionUtil.close(sqlSession);
        return rst;
    }


}
